package org.erepublik.company.controllers;

import org.erepublik.company.CompanyCalculator;
import org.erepublik.company.CountryData;
import org.erepublik.company.searchCriteria.CompanyFetch;
import org.erepublik.company.service.IProductsService;
import org.erepublik.company.service.JSONCountryDataProvider;
import org.erepublik.company.service.RawRegionsService;
import org.erepublik.company.service.XmlVatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/companyCalculator.html")
public class CompanyCalculatorController extends AbstractController {

    @Autowired // autumatycznie wstrzykiwany obiekt
    private IProductsService productService; 
    
    @Autowired
    private JSONCountryDataProvider dataService;  
    
    @Autowired
    private RawRegionsService rawRegionService;  
    
    @Autowired
    private XmlVatService vatService;  
    
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap model) {
        fillModelMap(model);
      
        model.addAttribute("company", null);
        model.addAttribute("companyFetch", new CompanyFetch());

        return "companyCalculator";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String showList(ModelMap model, @ModelAttribute("companyFetch") CompanyFetch criteria) {
        fillModelMap(model);

        CompanyCalculator company = new CompanyCalculator(criteria.getCompanyId(), productService, vatService,  
                    criteria.isMaxWellness(), criteria.isReadSalaries(), rawRegionService);
        company.calculateProfit();
        
        
        CountryData country = dataService.getCountryData(company.getCompany().getCountry());
        
        
        double currency = country.getGoldForCurrency();
        model.addAttribute("company", company);
        model.addAttribute("currency", currency);
        model.addAttribute("companyFetch", criteria);
        model.addAttribute("hideBottom", true);
        
        return "companyCalculator";
    }
    
}
